home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: alisa.org!wjjr
- From: wjjr@alisa.org (John J. Rushford)
- Subject: Re: hex to dec function?
- X-Newsreader: TIN [version 1.2 PL2]
- Organization: My place on the Front Range.
- Message-ID: <DoAE6F.2CD@alisa.org>
- References: <4gdh1b$bg@mailhost.mwmicro.com> <4hl8mk$1er@info.uah.edu> <313EE885.4511@cmt.lpr.mail.carel.fi> <4i87sm$7n0@info.uah.edu>
- Date: Fri, 15 Mar 1996 02:13:27 GMT
-
- Greg Bacon (gbacon@oreo) wrote:
- : Ari Lukumies (aril@cmt.lpr.mail.carel.fi) wrote:
- : : Greg Bacon wrote:
- : : >
-
- : : [snip]
-
- : : > Assuming you have a string representing some hexadecimal quantity and
- : : > you want to print its equivalent decimal value:
- : : >
- : : > #include <stdio.h>
- : : > #include <stdlib.h>
- : : >
- : : > /*
- : : > :
- : : > :
- : : > */
- : : > {
- : : > char *strHexVal = "20"; /* 32 auf decimal */
- : : >
- : : > (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
- : : > [snip]
-
- : : Just a quick sidenote: if you use strtol() in a printf call, use the
- : : correct format string, also: "%ld\n".
-
- : : Later,
- : : AriL
-
- : OK, OK.. you got me. I should have commented that I was assuming int
- : and long were of the same length (a trait not often found on the
- : braindead iAPX platform :).
-
- : The above works on most modern Unices.
-
- It didn't work on mine and why should it? How does strtol know that "20" is
- base 16? The following, Is what I think you were trying to convey:
-
- char *strHexVal = "0x20"; /* 32 auf decimal */
-
- printf ("%d\n", strtol (strHexVal, (char **) NULL, 16));
- ^^^
- not 10
-
- The following works too:
-
- char *strHexVal = "20";
-
- printf("%d\n",((((atol(strHexVal)) / 10) * 16) + ((atol(strHexVal) % 10))));
-
-
- regards
- --
- John J. Rushford
- Westminster, Colorado___/\_/\_
- wjjr@alisa.org (alisa.org is powered by FreeBSD)
-
-